package com.pm.property.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.pm.entity.Repairs;
import com.pm.exception.MyException;
import com.pm.property.biz.RepairsBiz;
import com.pm.utils.Constants;

/**
 * @author cjd
 *
 */
@Controller
@RequestMapping("/repairs")
public class RepairsController {
	@Resource
	private RepairsBiz repairsBiz;
	
	static Logger logger = Logger.getLogger(RepairsController.class);
	
	@RequestMapping("/preInsert")
	public String preInsert(){
		return "/property/addRepairs";
	}
	
	@RequestMapping("/insertSelestive")
	public String insertSelective(@RequestParam("fileAttach") MultipartFile[] files,Repairs repairs,HttpSession session) throws MyException{
		session.getAttribute("user");
		repairsBiz.insertSelective(repairs);
		try {
			for (MultipartFile multipartFile : files) {
				String fileName = UUID.randomUUID().toString();
				File newFile = new File(Constants.IMAGE_PATH + fileName);
				multipartFile.transferTo(newFile);
			}
		} 
		catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			throw new MyException("RepairsController insertSelective 异常");
		}
		return "";
	}
	@InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
                true));
    }
}
